package com.miaosha.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaosha.demo.exception.GlobalException;
import com.miaosha.demo.mapper.UserMapper;
import com.miaosha.demo.pojo.User;
import com.miaosha.demo.service.IUserService;
import com.miaosha.demo.utils.CookieUtil;
import com.miaosha.demo.utils.MD5Util;
import com.miaosha.demo.utils.UUIDUtil;
import com.miaosha.demo.utils.ValidatorUtil;
import com.miaosha.demo.vo.LoginVo;
import com.miaosha.demo.vo.RespBean;
import com.miaosha.demo.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.management.ObjectName;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *

*
* @author xiaoshuaila
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<String, Object>redisTemplate;
    @Override
    public RespBean doLogin(LoginVo loginVo,
                            HttpServletRequest request,
                            HttpServletResponse response) {
        String mobile= loginVo.getMobile();
        String password=loginVo.getPassword();
        User user=userMapper.selectById(mobile);
        if(null==user)
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        if(!MD5Util.formPassToDBPass(password,user.getSlat()).equals(user.getPassword()))
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        String ticket= UUIDUtil.uuid();
        redisTemplate.opsForValue().set("user:"+ticket,user);

        //request.getSession().setAttribute(ticket,user);

        CookieUtil.setCookie(request,response,"userTicket",ticket);
        return RespBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket,HttpServletRequest request,
                                HttpServletResponse response) {
        if(StringUtils.isEmpty(userTicket))return null;
        User user =
                (User)redisTemplate.opsForValue().get("user:" + userTicket);
        if(user!=null)
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        return user;
    }

    @Override
    public RespBean updatePassword(String userTicket,
                                   String password,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        User user = getUserByCookie(userTicket, request,
                response);
        if(user==null)
            throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST);
        user.setPassword(MD5Util.inputPassToDBPass(password,user.getSlat()));
        int result=userMapper.updateById(user);
        if(1==result) {
            redisTemplate.delete("user:" + userTicket);
            return RespBean.success();
        }

        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
    }
}
